package com.ruoyi.hr.service.impl.salaryadmin.salarychange;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.salaryadmin.SalaryAdminMapper;
import com.ruoyi.hr.mapper.salaryadmin.salarychange.SalaryChangeMapper;
import com.ruoyi.hr.service.salaryadmin.salarychange.SalaryChangeService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalaryChangeServiceImpl implements SalaryChangeService {

    @Autowired
    SalaryChangeMapper salaryChangeMapper;

    @Autowired
    SalaryAdminMapper salaryAdminMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map map, Authority authority) {
        List<Map<String, Object>> list = salaryAdminMapper.findProObject(map.get("pgid").toString());
        StringBuffer sql = new StringBuffer();
        for (Map map1 : list) {
            sql.append("sum (case 项目 when N'" + map1.get("title").toString() + "' then 数值 else null end)" + map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@新,");
            sql.append("sum (case 项目 when N'" + map1.get("title").toString() + "' then 原数值 else null end)" + map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@旧,");
        }

        List<Map<String, Object>> list1 = salaryChangeMapper.findData(map, sql.toString(), authority);
        //因为如果Map为null则没有键,页面会显示undefined

        for (Map map1 : list) {
            //循环薪资项目
            String newSalaryKey = map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@新";
            String oldSalaryKey = map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@旧";
            for (Map map2 : list1) {
                //循环人
                if (!map2.containsKey(newSalaryKey)) {
                    map2.put(newSalaryKey, 0);
                }
                if (!map2.containsKey(oldSalaryKey)) {
                    map2.put(oldSalaryKey, 0);
                }
            }

        }
        return list1;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findDataHistory(Map map, Authority authority) {
        List<Map<String, Object>> list = salaryAdminMapper.findProObject(map.get("pgid").toString());
        StringBuffer sql = new StringBuffer();
        for (Map map1 : list) {
            sql.append("sum (case 项目 when N'" + map1.get("title").toString() + "' then 数值 else null end)" + map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@新,");
            sql.append("sum (case 项目 when N'" + map1.get("title").toString() + "' then 原数值 else null end)" + map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@旧,");
        }

        List<Map<String, Object>> list1 = salaryChangeMapper.findDataHistory(map, sql.toString(), authority);
        //因为如果Map为null则没有键,页面会显示undefined

        for (Map map1 : list) {
            //循环薪资项目
            String newSalaryKey = map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@新";
            String oldSalaryKey = map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + "@@@旧";
            for (Map map2 : list1) {
                //循环人
                if (!map2.containsKey(newSalaryKey)) {
                    map2.put(newSalaryKey, 0);
                }
                if (!map2.containsKey(oldSalaryKey)) {
                    map2.put(oldSalaryKey, 0);
                }
            }
        }
        return list1;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateSalary(Map map) {
        return salaryChangeMapper.updateSalary(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findChangeEmp(Map map, Authority authority) {
        return salaryChangeMapper.findChangeEmp(map, authority);
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addSalaryChangeEmpProcedure(String allPeid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allPeid.split(",");
            String msg;//异常信息
            for (String peid : array) {
                //根据PEID取出人名
                String name = salaryChangeMapper.findNameByPeid(peid);
                sql = "declare @RetVal varchar(50) exec cSP_ChangeAdd " + peid + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = salaryChangeMapper.SalaryChangeEmpProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult deleteSalaryChangeEmpProcedure(String allId) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                String name = salaryChangeMapper.findNameByid(id);
                sql = "declare @RetVal varchar(50) exec cSP_ChangeDec " + id + ",@RetVal output select @RetVal";
                String retVal = salaryChangeMapper.SalaryChangeEmpProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS,"删除成功!");
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult updateSalaryDataProcedure(String allId) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                String name = salaryChangeMapper.findNameByid(id);
                sql = "cChange_Std " + id + "," + maps.get("id").toString();
                String retVal = salaryChangeMapper.SalaryChangeEmpProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS,"薪资更新成功");
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult threeProcedure(String allId, String type) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                String name = salaryChangeMapper.findNameByid(id);
                sql = "declare @RetVal varchar(50) exec " + type + " " + id + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = salaryChangeMapper.SalaryChangeEmpProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {

            if (type.equals("cSP_ChangeCheck")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "数据确认成功!");
            } else if (type.equals("cSP_ChangeCancel")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "取消确认成功!");
            } else if (type.equals("cSP_ChangeStart")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "变动处理成功!");
            }
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String findNameByAllid(String allId) {
        return salaryChangeMapper.findNameByAllid(allId);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int editEffdate(Map map) {
        return salaryChangeMapper.editEffdate(map);
    }
}